package put.cs.two.hied.netflix;

import java.io.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;

public class CreatePHPClassWithQualifier {

	public static void parseQualifierFile(File file) throws IOException {
		System.out.println("Start creating of PHP class...");
        BufferedReader in = new BufferedReader(new FileReader(file.getCanonicalPath()));
        String line;
        String movieId = null;

        BufferedWriter out = new BufferedWriter(new FileWriter("/home/ivas/netflix/qualifier.php"));
        out.write("<?php\n\ndefined('_JEXEC') or die('Restricted access');");
        out.write("\n\n$qualifier = array(");
        boolean first = true;
        boolean firstSubArray = true;
        boolean firstUserId = true;
        int fileIter = 0;
        while ((line = in.readLine()) != null) {
        	fileIter++;
        	if(fileIter % 50 == 0) {
        		System.out.println(" > parsing... line " + fileIter);
        	}
            if (!line.contains(":")) {
                TrainingSetEntry entry = new TrainingSetEntry(movieId);
                String[] words = line.split(",");
                entry.setUserId(words[0]);
                entry.setRating(words[1]);
                entry.setDate(words[2]);

                if (first) {
                	String closeSubArray = "), ";
                	if(firstSubArray) {
                		closeSubArray = "";
                		firstSubArray = false;
                	}
                	out.write(closeSubArray + "\n\t" + movieId + " => array(");
                    first = false;
                    firstUserId = true;
                }
                
                String userIdSeparator = ", ";
                if(firstUserId) {
                	userIdSeparator = "";
                	firstUserId = false;
                }
                out.write(userIdSeparator + entry.getUserId() + " => " + entry.getRating());
                
            } else {
                movieId = ProbeReader.trim(line);
                first = true;
            }
        }
        out.write(")\n);");
        in.close();
        out.close();
        System.out.println("Finish!");
    }
	
	public static void main(String[] args) {
		File input = new File("/home/ivas/netflix/orginal_data/qualifierResult2.txt");
		try {
			parseQualifierFile(input);
		} catch (IOException e) {}
	}

}
